package org.example.java8.lambda;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class FilterApple2 {
    //策略模式
    @FunctionalInterface
    public interface AppleFilter {
        boolean filter(Apple apple);
    }

    public static List<Apple> findApple(List<Apple> apples, AppleFilter appleFilter) {
        List<Apple> list = new ArrayList<>();
        for (Apple apple : apples) {
            if (appleFilter.filter(apple))
                list.add(apple);
        }
        return list;
    }

    public static class GreenAnd160WeightFilter implements AppleFilter {

        @Override
        public boolean filter(Apple apple) {
            return apple.getColor().equals("green") && apple.getWeight() >= 160;
        }
    }

    public static class YellowAnd150WeightFilter implements AppleFilter {
        @Override
        public boolean filter(Apple apple) {
            return apple.getColor().equals("yellow") && apple.getWeight() >= 150;
        }
    }

    public static void main(String[] args) throws InterruptedException {
        List<Apple> list = Arrays.asList(new Apple("green", 150), new Apple("yellow", 170), new Apple("green", 200));
        List<Apple> result = findApple(list, new GreenAnd160WeightFilter());
        System.out.println(result);

        //匿名类写法
        List<Apple> greenList = findApple(list, new AppleFilter() {
            @Override
            public boolean filter(Apple apple) {
                return apple.getColor().equals("green");
            }
        });
        System.out.println(greenList);

        //Lambda表达式
        //List<Apple> lambdaResult = findApple(list, (Apple apple) -> {
        //    return apple.getColor().equals("yellow");
        //});
        List<Apple> lambdaResult = findApple(list, apple -> apple.getColor().equals("yellow"));
        System.out.println(lambdaResult);

        //Runnable是一个FunctionalInterface函数接口
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName());
            }
        }).start();

        new Thread(() -> System.out.println(Thread.currentThread().getName())).start();

        Thread.currentThread().join();
    }
}
